
const {GeneratedType, Registry, DirectSecp256k1HdWallet} = require("@cosmjs/proto-signing");

const { IndexedTx,SigningStargateClient, StargateClient } = require("@cosmjs/stargate")
const { stringToPath } = require("@cosmjs/crypto")

const { MsgSend } = require("cosmjs-types/cosmos/bank/v1beta1/tx");
const { Tx } = require("cosmjs-types/cosmos/tx/v1beta1/tx");
const { MsgReceiveMessage } = require('./message');


const config = require('../config')

const rpc = config.noble.url;
const cctpTypes = [
    ["/circle.cctp.v1.MsgReceiveMessage", MsgReceiveMessage],
];

function createDefaultRegistry() {
    return new Registry(cctpTypes)
}


const mint = async (message, attestation) => {
    console.log('config.mnemonic: ', config.MNEMONIC);

    const mnemonic = config.MNEMONIC;
    const wallet = await DirectSecp256k1HdWallet.fromMnemonic(
        mnemonic,
        {
            hdPaths: [stringToPath("m/44'/118'/0'/0/0")],
            prefix: "noble"
        }
    );

    const [account] = await wallet.getAccounts();

    console.log(account.address)

    const client = await SigningStargateClient.connectWithSigner(rpc, wallet, {registry: createDefaultRegistry()});

    const msg = {
        typeUrl:"/circle.cctp.v1.MsgReceiveMessage",
        value:{
            from:"",
            message:message,
            attestation:attestation
        }
    }
    const fee = {
        amount: [
            {
                denom: "uusdc",
                amount: "200000",
            },
        ],
        gas: "200000",
    };
    const memo = "haolf";
    const result = await client.signAndBroadcast(
        account.address,
        [msg],
        fee,
        memo
    );
    console.log("result: ", result)

}
const main = async() => {
    console.log('config.mnemonic: ', config.MNEMONIC);

    const mnemonic = config.MNEMONIC;
    const wallet = await DirectSecp256k1HdWallet.fromMnemonic(
        mnemonic,
        {
            hdPaths: [stringToPath("m/44'/118'/0'/0/0")],
            prefix: "noble"
        }
    );

    const [account] = await wallet.getAccounts();

    console.log(account.address)

    const client = await SigningStargateClient.connectWithSigner(rpc, wallet, {registry: createDefaultRegistry()});

    const msg = {
        typeUrl:"/circle.cctp.v1.MsgReceiveMessage",
        value:{
            from:account.address,
            message:"0x000000000000000000000004000000000003ace2000000000000000000000000d0c3da58f55358142b8d3e06c1c30c5c6114efe800000000000000000000000057d4eaf1091577a6b7d121202afbd2808134f11700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007865c6e87b9f70255377e024ace6630c1eaa37f0000000000000000000000006a769dbfb6a90de5671102d35285a412d0f963e80000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ef48c3ab7f184e469504bdbd9c07883ab4427c66",
            attestation:"0xaf16f1c051310000d6b0204558d026e58ac5dbe607faed2723a1a3378859046d20367faeba595ffb371567fda922a6d0e5c938555e9a1cec0261f8d63ce126261b9799b96c0abb2627b8090b2f455e318530bd496b58cb36c47a46f0f512663cad71a47bd25614e2d947c65d3259201ca47e85164fce0a421f47e01bcbda3c4a191c"
        }
    }
    const fee = {
        amount: [
            {
                denom: "uusdc",
                amount: "200000",
            },
        ],
        gas: "200000",
    };
    const memo = "haolf";
    const result = await client.signAndBroadcast(
        account.address,
        [msg],
        fee,
        memo
    );
    console.log("result: ", result)









}
//main();
module.exports = mint